//
//  WGQAlbumDateHeaderViewCollectionViewCell.m
//  meiang_edu
//
//  Created by gq w on 2018/10/10.
//  Copyright © 2018 com.meiang.meiang_edu. All rights reserved.
//

#import "WGQAlbumDateHeaderViewCollectionViewCell.h"
#import "WGQAlbumDateHeaderCollectionViewCell.h"

@interface WGQAlbumDateHeaderViewCollectionViewCell ()<UICollectionViewDelegate,UICollectionViewDataSource>

@property (nonatomic,strong) UICollectionView * calendarCollectionView;
@property (nonatomic,strong) NSMutableArray * collectionArray;
@property (nonatomic,strong) NSArray * weeekArray;
@property (nonatomic,assign) NSInteger dayInWeek;

@end

@implementation WGQAlbumDateHeaderViewCollectionViewCell

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame])
    {
        self.collectionArray = [NSMutableArray array];
        self.weeekArray = @[@"日",@"一",@"二",@"三",@"四",@"五",@"六"];
        [self addSubViews];
    }
    return self;
}

- (void)addSubViews
{
    // 日期
    CGFloat width = kScreenWidth / 7.1;
    UICollectionViewFlowLayout * dateLayout = [[UICollectionViewFlowLayout alloc] init];
    dateLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
    dateLayout.itemSize = CGSizeMake(width, 70 * kScale);
    dateLayout.minimumLineSpacing = 0;
    dateLayout.minimumInteritemSpacing = 0;
    self.calendarCollectionView = [[UICollectionView alloc] initWithFrame:self.bounds collectionViewLayout:dateLayout];
    [self addSubview:self.calendarCollectionView];
    self.calendarCollectionView.backgroundColor = [UIColor whiteColor];
    self.calendarCollectionView.delegate = self;
    self.calendarCollectionView.dataSource = self;
    [self.calendarCollectionView registerClass:[WGQAlbumDateHeaderCollectionViewCell class] forCellWithReuseIdentifier:[NSString stringWithUTF8String:object_getClassName([WGQAlbumDateHeaderCollectionViewCell class])]];
    [self addSubview:self.calendarCollectionView];
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return 7;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    WGQAlbumDateHeaderCollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:[NSString stringWithUTF8String:object_getClassName([WGQAlbumDateHeaderCollectionViewCell class])] forIndexPath:indexPath];
    [cell setObject:self.collectionArray[indexPath.row]];
    [cell setWeek:self.weeekArray[indexPath.row]];
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (self.item_click_block)
    {
        self.item_click_block(self.collectionArray[indexPath.row]);
    }
    [self.collectionArray enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop)
     {
         WGQAlbumDateHeaderModel * model = obj;
         if (idx == indexPath.row)
         {
             model.isSelect = YES;
         }
         else
         {
             model.isSelect = NO;
         }
     }];
    [self.calendarCollectionView reloadData];
}

- (void)setObject:(id)object
{
    if ([object isKindOfClass:[NSArray class]])
    {
        NSArray * array = object;
        if (self.collectionArray.count > 0)
        {
            [self.collectionArray removeAllObjects];
        }
        [self.collectionArray addObjectsFromArray:array];
        [self.calendarCollectionView reloadData];
    }
}

@end
